<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import dayjs from 'dayjs'

//获取当前日期
const current = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
const currentTime = ref(current)
let timerID = ref()
onMounted(() => {
  timerID.value = setInterval(() => {
    currentTime.value = dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
  }, 1000)
})
onBeforeUnmount(() => {
  clearInterval(timerID.value)
  timerID.value = null
})
</script>
<template>
  <div class="current-time">{{ currentTime }}</div>
</template>
<style lang="less" scoped>
.current-time {
  position: absolute;
  right: 10px;
  top: 0;
  color: #fff;
  font-size: 22px;
}
</style>
